package com.config;

import org.springframework.cloud.openfeign.support.SpringDecoder;
import feign.Response;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.*;

/**
 * @author 惊云
 * @date 2024/1/13 15:42
 */
public class TraceDecoder extends SpringDecoder {

    private ObjectFactory<HttpMessageConverters> messageConverters;

    public TraceDecoder(ObjectFactory<HttpMessageConverters> messageConverters) {
        super(messageConverters);
    }

    @Override
    public Object decode(Response response, Type type) throws IOException {
        Map<String, Collection<String>> headers = response.headers();
        Collection<String> tranceInfoList = headers.get("tranceInfo");
        if(!CollectionUtils.isEmpty(tranceInfoList)){
            List<String> tt = new ArrayList<>();
            tt.addAll(tranceInfoList);
            FeginTraceContext.traceContext.set(tt.get(0));
        }

        return super.decode(response,type);
    }

}
